package com.javaee.xiangyang.service.impl;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.GoodsComment;
import com.javaee.xiangyang.dto.UserComment;
import com.javaee.xiangyang.entity.Comment;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.mapper.CommentMapper;
import com.javaee.xiangyang.mapper.GoodsMapper;
import com.javaee.xiangyang.mapper.UserMapper;
import com.javaee.xiangyang.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    GoodsMapper goodsMapper;

    /**
     * 新增评论
     */
    @Override
    @Transactional
    public ServiceResultEnum addComment(Comment comment) {
        if (userMapper.findByUserAccount(new User(comment.getCommentUserAccount())) != null) {
            //此用户存在
            if (goodsMapper.findGoodsByGoodsId(new Goods(comment.getCommentGoodsId())) != null) {
                //此商品存在
                comment.setCommentTime(new Date());
                commentMapper.addComment(comment);
                return ServiceResultEnum.SUCCESS;
            }
            return ServiceResultEnum.GOODS_NOT_EXIST;
        }
        //用户不存在
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 查看所有评论
     */
    @Override
    public List<Comment> findAllComment() {
        return commentMapper.findAllComment();
    }

    /**
     * 用户在手机上查看自己所有的评论
     * 此处需要新的封装类  封装DTO
     */
    @Override
    public List<UserComment> findAllCommentByUserAccount(User user) {
        return commentMapper.findAllUserCommentByUserId(user);
    }

    /**
     * 管理员封禁或解封当前评论
     */
    @Override
    @Transactional
    public ServiceResultEnum updateStateByCommentId(Comment comment) {
        commentMapper.updateStateByCommentId(comment);
        return ServiceResultEnum.SUCCESS;
    }

    /**
     * 用户删除自己的评论
     */
    @Override
    @Transactional
    public ServiceResultEnum deleteCommentByCommentId(Comment comment) {
        if (commentMapper.findCommentByCommentId(comment) != null) {
            //此评论存在
            if (commentMapper.findCommentByCommentId(comment).getCommentUserAccount().equals(comment.getCommentUserAccount())) {
                //此评论是此用户产生
                commentMapper.deleteCommentByCommentId(comment);
                return ServiceResultEnum.SUCCESS;
            }
            return ServiceResultEnum.COMMENT_NOT_EXIST_USER;
        }
        return ServiceResultEnum.COMMENT_NOT_EXIST;
    }

    /**
     * 管理员查看某一位用户的评论
     */
    @Override
    public List<Comment> findAllCommentByUser(User user) {
        return commentMapper.findAllCommentByUserAccount(user);
    }

    /**
     * 管理员查看某商品评论
     */
    @Override
    public List<Comment> findAllCommentByGoods(Goods goods) {
        return commentMapper.findAllCommentByGoodsId(goods);
    }
}
